<template>
  <div class="app-container">
    <el-form :model="queryParam" ref="form" :rules="rules" :inline="true">
      <el-form-item label="发送人Id：" prop="sendUserId">
        <el-input v-model="queryParam.sendUserId"></el-input>
      </el-form-item>
      <el-form-item label="通知类型：">
        <el-select v-model="queryParam.informType" clearable>
          <el-option v-for="item in allTypes" :key="item.id" :label="item.categoryName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="大致内容：" >
        <el-input v-model="queryParam.content" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="listLoading" :data="tableData" border fit highlight-current-row style="width: 100%">
      <el-table-column prop="informId" label="Id" width="100" />
      <el-table-column prop="sendUserId" label="发送人的Id" width="100" />
      <el-table-column prop="content" label="发送内容" show-overflow-tooltip />
<!--      <el-table-column prop="isRead" label="是否已读" width="100" />-->
      <el-table-column prop="informName" label="通知类型" width="100" />
      <el-table-column prop="createTime" label="发送时间" width="160px" />
<!--      <el-table-column prop="modifyTime" label="修改时间" width="160px" />-->
      <el-table-column label="操作" align="center" width="160px">
        <template slot-scope="{ row }">
          <el-button size="mini" type="primary" @click="showDetails(row)">详情</el-button>
<!--          <el-button size="mini" type="primary" @click="dialogVisible = true">详情</el-button>-->
          <el-button size="mini" type="danger" @click="deleteTask(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParam.pageIndex" :limit.sync="queryParam.pageSize" @pagination="search" />

    <el-dialog title="接收该消息的人有：" :visible.sync="dialogVisible" width="70%" >
      <el-form :model="informConf" ref="queryForm" :inline="true" :rules="rules">
        <el-form-item label="接收人Id：" prop="userId">
          <el-input v-model="informConf.userId" clearable></el-input>
        </el-form-item>
        <el-form-item label="接收人姓名：">
          <el-input v-model="informConf.userName" clearable></el-input>
        </el-form-item>
        <el-form-item label="是否已读：">
          <el-select v-model="informConf.isread">
            <el-option v-for="item in [{id: 1, name: '未读'}, {id: 0, name: '已读'}]" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="大致内容：">
          <el-input v-model="informConf.content" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="sendInformBefore()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="informUser" border fit highlight-current-row style="width: 100%">
        <el-table-column type="selection" width="35"></el-table-column>
        <el-table-column prop="id" label="Id" width="90px"/>
        <el-table-column prop="informId" label="消息Id"  width="120px"/>
        <el-table-column prop="userId" label="接收人Id"  width="120px"/>
        <el-table-column prop="userName" label="接收人姓名" width="100px"/>
        <el-table-column prop="content" label="发送内容" show-overflow-tooltip/>
<!--        <el-table-column prop="isread" label="是否已读" width="80px" />-->
        <el-table-column label="是否已读" width="80px">
          <template slot-scope="{ row }">
            <span :style="{ color: row.isread === 1 ? 'red' : 'green' }">
              {{ row.isread === 1 ? '未读' : '已读' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="发送时间"  width="160px"/>
        <el-table-column prop="modifyTime" label="读取时间" width="160px"/>
      </el-table>
      <pagination v-show="informAll.total>0" :total="informAll.total"
                  :page.sync="informConf.pageIndex" :limit.sync="informConf.pageSize"
                  @pagination="sendInform()"/>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import informApi from '@/api/inform'

export default {
  components: { Pagination },
  data () {
    return {
      dialogVisible: false,
      queryParam: {
        pageIndex: 1,
        pageSize: 10,
        sendUserId: null,
        informType: null,
        content: null
      },
      acceptInfor: {
        pageindex: 1,
        pageSize: 10
      },
      informUser: [],
      showDialog: false,
      listLoading: true,
      tableData: [],
      total: 0,
      informConf: {
        pageIndex: 1,
        pageSize: 10,
        informId: null,
        userId: null,
        userName: null,
        isread: null,
        content: null
      },
      informAll: {
        list: [],
        total: 0
      },
      allTypes: [],
      rules: {
        sendUserId: [
          { type: 'string', message: '发送人Id必须是数字', trigger: 'blur' },
          { validator: this.validateSendUserId, trigger: 'blur' }
        ],
        userId: [
          { type: 'string', message: '接收人Id必须是数字', trigger: 'blur' },
          { validator: this.validateUserId, trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.search()
    this.selectAllTypes()
  },
  methods: {
    validateUserId (rule, value, callback) {
      if (value === null || value === '') {
        callback()
      } else {
        const numValue = Number(value)
        console.log(value)
        if (isNaN(numValue)) {
          callback(new Error('接收人Id必须是数字'))
        } else if (numValue <= 0) {
          callback(new Error('接收人Id必须大于0'))
        } else {
          callback()
        }
      }
    },
    validateSendUserId (rule, value, callback) {
      if (value === null || value === '') {
        callback()
      } else {
        const numValue = Number(value)
        if (isNaN(numValue)) {
          callback(new Error('发送人Id必须是数字'))
        } else if (numValue <= 0) {
          callback(new Error('发送人Id必须大于0'))
        } else {
          callback()
        }
      }
    },
    selectAllTypes () {
      informApi.selectAllInformType().then(data => {
        console.log(data.response)
        this.allTypes = data.response
      })
    },
    showDetails (row) {
      this.dialogVisible = true
      this.informConf.informId = row.informId
      this.sendInform()
    },
    sendInformBefore () {
      this.$refs.queryForm.validate((valid) => {
        if (valid) {
          this.sendInform()
        } else {
          return false
        }
      })
    },
    sendInform () {
      informApi.selectAcceptInform(this.informConf).then(data => {
        this.informAll = data.response
        const re = data.response
        this.informUser = re.list
      })
    },
    deleteTask (row) {
      const { informId } = row
      informApi.deleteTask(informId).then(({ code, message }) => {
        if (code === 1) {
          this.search()
          this.$message.success('删除成功')
        } else {
          this.$message.error('删除失败: ' + message)
        }
      })
    },
    search () {
      this.listLoading = true
      informApi.pageList(this.queryParam).then(data => {
        const re = data.response
        this.tableData = re.list
        console.log(data)
        this.total = re.total
        this.queryParam.pageIndex = re.pageNum
        this.listLoading = false
      })
    },
    submitForm () {
      this.queryParam.pageIndex = 1
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.search()
        } else {
          return false
        }
      })
    }
  }
}
</script>
